home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / djgpp / src / texinfo-.1 / info / makedoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-29  |  12.1 KB  |  483 lines

  1. /* makedoc.c -- Make DOC.C and FUNS.H from input files. */
  2.  
  3. /* This file is part of GNU Info, a program for reading online documentation
  4.    stored in Info format.
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.    Written by Brian Fox (bfox@ai.mit.edu). */
  23.  
  24. /* This program grovels the contents of the source files passed as arguments
  25.    and writes out a file of function pointers and documentation strings, and
  26.    a header file which describes the contents.  This only does the functions
  27.    declared with DECLARE_INFO_COMMAND. */
  28.  
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/file.h>
  33. #include <sys/stat.h>
  34. #include "general.h"
  35.  
  36. #if !defined (O_RDONLY)
  37. #if defined (HAVE_SYS_FCNTL_H)
  38. #include <sys/fcntl.h>
  39. #else /* !HAVE_SYS_FCNTL_H */
  40. #include <fcntl.h>
  41. #endif /* !HAVE_SYS_FCNTL_H */
  42. #endif /* !O_RDONLY */
  43.  
  44. extern void *xmalloc (), *xrealloc ();
  45. static void fatal_file_error ();
  46.  
  47. /* Name of the header file which receives the declarations of functions. */
  48. static char *funs_filename = "funs.h";
  49.  
  50. /* Name of the documentation to function pointer file. */
  51. static char *doc_filename = "doc.c";
  52.  
  53. static char *doc_header[] = {
  54.   "/* doc.c -- Generated structure containing function names and doc strings.",
  55.   "",
  56.   "   This file was automatically made from various source files with the",
  57.   "   command \"%s\".  DO NOT EDIT THIS FILE, only \"%s.c\".",
  58.   (char *)NULL
  59. };
  60.  
  61. static char *doc_header_1[] = {
  62.   "   An entry in the array FUNCTION_DOC_ARRAY is made for each command",
  63.   "   found in the above files; each entry consists of a function pointer,",
  64. #if defined (NAMED_FUNCTIONS)
  65.   "   a string which is the user-visible name of the function,",
  66. #endif /* NAMED_FUNCTIONS */
  67.   "   and a string which documents its purpose. */",
  68.   "",
  69.   "#include \"doc.h\"",
  70.   "#include \"funs.h\"",
  71.   "",
  72.   "FUNCTION_DOC function_doc_array[] = {",
  73.   "",
  74.   (char *)NULL
  75. };
  76.  
  77. /* How to remember the locations of the functions found so that Emacs
  78.    can use the information in a tag table. */
  79. typedef struct {
  80.   char *name;            /* Name of the tag. */
  81.   int line;            /* Line number at which it appears. */
  82.   long char_offset;        /* Character offset at which it appears. */
  83. } EMACS_TAG;
  84.  
  85. typedef struct {
  86.   char *filename;        /* Name of the file containing entries. */
  87.   long entrylen;        /* Total number of characters in tag block. */
  88.   EMACS_TAG **entries;        /* Entries found in FILENAME. */
  89.   int entries_index;
  90.   int entries_slots;
  91. } EMACS_TAG_BLOCK;
  92.  
  93. EMACS_TAG_BLOCK **emacs_tags = (EMACS_TAG_BLOCK **)NULL;
  94. int emacs_tags_index = 0;
  95. int emacs_tags_slots = 0;
  96.  
  97. #define DECLARATION_STRING "\nDECLARE_INFO_COMMAND"
  98.  
  99. static void process_one_file ();
  100. static void maybe_dump_tags ();
  101. static FILE *must_fopen ();
  102.  
  103. int
  104. main (argc, argv)
  105.      int argc;
  106.      char **argv;
  107. {
  108.   register int i;
  109.   int tags_only = 0;
  110.   FILE *funs_stream, *doc_stream;
  111.  
  112.   for (i = 1; i < argc; i++)
  113.     if (strcmp (argv[i], "-tags") == 0)
  114.       {
  115.     tags_only++;
  116.     break;
  117.       }
  118.  
  119.   if (tags_only)
  120.     {
  121. #if defined (__MSDOS__)
  122.       funs_filename = "nul";
  123.       doc_filename = "nul";
  124. #else /* __MSDOS__ */
  125.       funs_filename = "/dev/null";
  126.       doc_filename = "/dev/null";
  127. #endif /* __MSDOS__ */
  128.     }
  129.   
  130.   funs_stream = must_fopen (funs_filename, "w");
  131.   doc_stream = must_fopen (doc_filename, "w");
  132.  
  133.   fprintf (funs_stream,
  134.        "/* %s -- Generated declarations for Info commands. */\n",
  135.        funs_filename);
  136.  
  137.   for (i = 0; doc_header[i]; i++)
  138.     {
  139.       fprintf (doc_stream, doc_header[i], argv[0], argv[0]);
  140.       fprintf (doc_stream, "\n");
  141.     }
  142.  
  143.   fprintf (doc_stream,
  144.        "   Source files groveled to make this file include:\n\n");
  145.  
  146.   for (i = 1; i < argc; i++)
  147.     fprintf (doc_stream, "\t%s\n", argv[i]);
  148.  
  149.   fprintf (doc_stream, "\n");
  150.  
  151.   for (i = 0; doc_header_1[i]; i++)
  152.     fprintf (doc_stream, "%s\n", doc_header_1[i]);
  153.  
  154.  
  155.   for (i = 1; i < argc; i++)
  156.     {
  157.       char *curfile;
  158.       curfile = argv[i];
  159.  
  160.       if (*curfile == '-')
  161.     continue;
  162.  
  163.       fprintf (doc_stream, "/* Commands found in \"%s\". */\n", curfile);
  164.       fprintf (funs_stream, "\n/* Functions declared in \"%s\". */\n",
  165.            curfile);
  166.  
  167.       process_one_file (curfile, doc_stream, funs_stream);
  168.     }
  169.  
  170.   fprintf (doc_stream,
  171.        "   { (VFunction *)NULL, (char *)NULL, (char *)NULL }\n};\n");
  172.  
  173.   fclose (funs_stream);
  174.   fclose (doc_stream);
  175.  
  176.   if (tags_only)
  177.     maybe_dump_tags (stdout);
  178.   exit (0);
  179. }
  180.  
  181. /* Dumping out the contents of an Emacs tags table. */
  182. static void
  183. maybe_dump_tags (stream)
  184.      FILE *stream;
  185. {
  186.   register int i;
  187.  
  188.   /* Print out the information for each block. */
  189.   for (i = 0; i < emacs_tags_index; i++)
  190.     {
  191.       register int j;
  192.       register EMACS_TAG_BLOCK *block;
  193.       register EMACS_TAG *etag;
  194.       long block_len;
  195.  
  196.       block_len = 0;
  197.       block = emacs_tags[i];
  198.  
  199.       /* Calculate the length of the dumped block first. */
  200.       for (j = 0; j < block->entries_index; j++)
  201.     {
  202.       char digits[30];
  203.       etag = block->entries[j];
  204.       block_len += 3 + strlen (etag->name);
  205.       sprintf (digits, "%d,%d", etag->line, etag->char_offset);
  206.       block_len += strlen (digits);
  207.     }
  208.  
  209.       /* Print out the defining line. */
  210.       fprintf (stream, "\f\n%s,%d\n", block->filename, block_len);
  211.  
  212.       /* Print out the individual tags. */
  213.       for (j = 0; j < block->entries_index; j++)
  214.     {
  215.       etag = block->entries[j];
  216.  
  217.       fprintf (stream, "%s,\177%d,%d\n",
  218.            etag->name, etag->line, etag->char_offset);
  219.     }
  220.     }
  221. }
  222.  
  223. /* Keeping track of names, line numbers and character offsets of functions
  224.    found in source files. */
  225. static EMACS_TAG_BLOCK *
  226. make_emacs_tag_block (filename)
  227.      char *filename;
  228. {
  229.   EMACS_TAG_BLOCK *block;
  230.  
  231.   block = (EMACS_TAG_BLOCK *)xmalloc (sizeof (EMACS_TAG_BLOCK));
  232.   block->filename = savestring (filename);
  233.   block->entrylen = 0;
  234.   block->entries = (EMACS_TAG **)NULL;
  235.   block->entries_index = 0;
  236.   block->entries_slots = 0;
  237.   return (block);
  238. }
  239.  
  240. static void
  241. add_tag_to_block (block, name, line, char_offset)
  242.      EMACS_TAG_BLOCK *block;
  243.      char *name;
  244.      int line;
  245.      long char_offset;
  246. {
  247.   EMACS_TAG *tag;
  248.  
  249.   tag = (EMACS_TAG *)xmalloc (sizeof (EMACS_TAG));
  250.   tag->name = name;
  251.   tag->line = line;
  252.   tag->char_offset = char_offset;
  253.   add_pointer_to_array (tag, block->entries_index, block->entries,
  254.             block->entries_slots, 50, EMACS_TAG *);
  255. }
  256.  
  257. /* Read the file represented by FILENAME into core, and search it for Info
  258.    function declarations.  Output the declarations in various forms to the
  259.    DOC_STREAM and FUNS_STREAM. */
  260. static void
  261. process_one_file (filename, doc_stream, funs_stream)
  262.      char *filename;
  263.      FILE *doc_stream, *funs_stream;
  264. {
  265.   int descriptor, decl_len;
  266.   char *buffer, *decl_str;
  267.   struct stat finfo;
  268.   long offset;
  269.   EMACS_TAG_BLOCK *block;
  270.  
  271.   if (stat (filename, &finfo) == -1)
  272.     fatal_file_error (filename);
  273.  
  274.   descriptor = open (filename, O_RDONLY, 0666);
  275.  
  276.   if (descriptor == -1)
  277.     fatal_file_error (filename);
  278.  
  279.   buffer = (char *)xmalloc (1 + finfo.st_size);
  280.   read (descriptor, buffer, finfo.st_size);
  281.   close (descriptor);
  282.  
  283.   offset = 0;
  284.   decl_str = DECLARATION_STRING;
  285.   decl_len = strlen (decl_str);
  286.  
  287.   block = make_emacs_tag_block (filename);
  288.  
  289.   while (1)
  290.     {
  291.       long point = 0;
  292.       long line_start = 0;
  293.       int line_number = 0;
  294.  
  295.       char *func, *doc;
  296. #if defined (NAMED_FUNCTIONS)
  297.       char *func_name;
  298. #endif /* NAMED_FUNCTIONS */
  299.  
  300.       for (; offset < (finfo.st_size - decl_len); offset++)
  301.     {
  302.       if (buffer[offset] == '\n')
  303.         {
  304.           line_number++;
  305.           line_start = offset + 1;
  306.         }
  307.  
  308.       if (strncmp (buffer + offset, decl_str, decl_len) == 0)
  309.         {
  310.           offset += decl_len;
  311.           point = offset;
  312.           break;
  313.         }
  314.     }
  315.  
  316.       if (!point)
  317.     break;
  318.  
  319.       /* Skip forward until we find the open paren. */
  320.       while (point < finfo.st_size)
  321.     {
  322.       if (buffer[point] == '\n')
  323.         {
  324.           line_number++;
  325.           line_start = point + 1;
  326.         }
  327.       else if (buffer[point] == '(')
  328.         break;
  329.  
  330.       point++;
  331.     }
  332.  
  333.       while (point++ < finfo.st_size)
  334.     {
  335.       if (!whitespace_or_newline (buffer[point]))
  336.         break;
  337.       else if (buffer[point] == '\n')
  338.         {
  339.           line_number++;
  340.           line_start = point + 1;
  341.         }
  342.     }
  343.  
  344.       if (point >= finfo.st_size)
  345.     break;
  346.  
  347.       /* Now looking at name of function.  Get it. */
  348.       for (offset = point; buffer[offset] != ','; offset++);
  349.       func = (char *)xmalloc (1 + (offset - point));
  350.       strncpy (func, buffer + point, offset - point);
  351.       func[offset - point] = '\0';
  352.  
  353.       /* Remember this tag in the current block. */
  354.       {
  355.     char *tag_name;
  356.  
  357.     tag_name = (char *)xmalloc (1 + (offset - line_start));
  358.     strncpy (tag_name, buffer + line_start, offset - line_start);
  359.     tag_name[offset - line_start] = '\0';
  360.     add_tag_to_block (block, tag_name, line_number, point);
  361.       }
  362.  
  363. #if defined (NAMED_FUNCTIONS)
  364.       /* Generate the user-visible function name from the function's name. */
  365.       {
  366.     register int i;
  367.     char *name_start;
  368.  
  369.     name_start = func;
  370.  
  371.     if (strncmp (name_start, "info_", 5) == 0)
  372.       name_start += 5;
  373.  
  374.     func_name = savestring (name_start);
  375.  
  376.     /* Fix up "ea" commands. */
  377.     if (strncmp (func_name, "ea_", 3) == 0)
  378.       {
  379.         char *temp_func_name;
  380.  
  381.         temp_func_name = (char *)xmalloc (10 + strlen (func_name));
  382.         strcpy (temp_func_name, "echo_area_");
  383.         strcat (temp_func_name, func_name + 3);
  384.         free (func_name);
  385.         func_name = temp_func_name;
  386.       }
  387.  
  388.     for (i = 0; func_name[i]; i++)
  389.       if (func_name[i] == '_')
  390.         func_name[i] = '-';
  391.       }
  392. #endif /* NAMED_FUNCTIONS */
  393.  
  394.       /* Find doc string. */
  395.       point = offset + 1;
  396.  
  397.       while (point < finfo.st_size)
  398.     {
  399.       if (buffer[point] == '\n')
  400.         {
  401.           line_number++;
  402.           line_start = point + 1;
  403.         }
  404.  
  405.       if (buffer[point] == '"')
  406.         break;
  407.       else
  408.         point++;
  409.     }
  410.  
  411.       offset = point + 1;
  412.  
  413.       while (offset < finfo.st_size)
  414.     {
  415.       if (buffer[offset] == '\n')
  416.         {
  417.           line_number++;
  418.           line_start = offset + 1;
  419.         }
  420.  
  421.       if (buffer[offset] == '\\')
  422.         offset += 2;
  423.       else if (buffer[offset] == '"')
  424.         break;
  425.       else
  426.         offset++;
  427.     }
  428.  
  429.       offset++;
  430.       if (offset >= finfo.st_size)
  431.     break;
  432.  
  433.       doc = (char *)xmalloc (1 + (offset - point));
  434.       strncpy (doc, buffer + point, offset - point);
  435.       doc[offset - point] = '\0';
  436.  
  437. #if defined (NAMED_FUNCTIONS)
  438.       fprintf (doc_stream, "   { %s, \"%s\", %s },\n", func, func_name, doc);
  439.       free (func_name);
  440. #else /* !NAMED_FUNCTIONS */
  441.       fprintf (doc_stream, "   { %s, %s },\n", func, doc);
  442. #endif /* !NAMED_FUNCTIONS */
  443.  
  444.       fprintf (funs_stream, "extern void %s ();\n", func);
  445.       free (func);
  446.       free (doc);
  447.     }
  448.   free (buffer);
  449.  
  450.   /* If we created any tags, remember this file on our global list.  Otherwise,
  451.      free the memory already allocated to it. */
  452.   if (block->entries)
  453.     add_pointer_to_array (block, emacs_tags_index, emacs_tags,
  454.               emacs_tags_slots, 10, EMACS_TAG_BLOCK *);
  455.   else
  456.     {
  457.       free (block->filename);
  458.       free (block);
  459.     }
  460. }
  461.  
  462. static void
  463. fatal_file_error (filename)
  464.      char *filename;
  465. {
  466.   fprintf (stderr, "Couldn't manipulate the file %s.\n", filename);
  467.   exit (2);
  468. }
  469.  
  470. static FILE *
  471. must_fopen (filename, mode)
  472.      char *filename, *mode;
  473. {
  474.   FILE *stream;
  475.  
  476.   stream = fopen (filename, mode);
  477.   if (!stream)
  478.     fatal_file_error (filename);
  479.  
  480.   return (stream);
  481. }
  482.  
  483.